from gis.common import track_logging

from django.utils.decorators import method_decorator

from gis.common.django_ext.decorators import validate_parameters
from gis.common.django_ext.views import BaseView, PaginationResponse
from gis.admin.decorators import require_login, check_permission
from itpay.server_admin.decorators import require_vcode
from itpay.core.mch import service as mch_service
from .mch_forms import *

_LOGGER = track_logging.getLogger(__name__)


class MchView(BaseView):
    @method_decorator(require_login)
    # @method_decorator(check_permission('mch_get'))
    @method_decorator(validate_parameters(ListMchSchema))
    def get(self, request, cleaned_data):
        total, mch = mch_service.list_mch(**cleaned_data)
        return PaginationResponse(total, mch)

    @method_decorator(require_login)
    @method_decorator(require_vcode)
    @method_decorator(check_permission('mch_add'))
    @method_decorator(validate_parameters(AddMchSchema))
    def post(self, request, cleaned_data):
        mch = mch_service.add_mch(**cleaned_data)
        return mch


class MchDetailView(BaseView):
    @method_decorator(require_login)
    @method_decorator(check_permission('mch_get'))
    def get(self, request, mch_id):
        mch = mch_service.get_mch(mch_id)
        return mch

    @method_decorator(require_login)
    @method_decorator(require_vcode)
    @method_decorator(check_permission('mch_update'))
    @method_decorator(validate_parameters(UpdateMchSchema))
    def put(self, request, mch_id, cleaned_data):
        mch_service.update_mch(mch_id, **cleaned_data)
        return dict(id=mch_id)


class MchEnableView(BaseView):
    @method_decorator(require_login)
    @method_decorator(require_vcode)
    @method_decorator(check_permission('mch_enable'))
    def put(self, request, mch_id):
        mch_service.enable_mch(mch_id)
        return dict(id=mch_id)


class MchDisableView(BaseView):
    @method_decorator(require_login)
    @method_decorator(require_vcode)
    @method_decorator(check_permission('mch_disable'))
    def put(self, request, mch_id):
        mch_service.disable_mch(mch_id)
        return dict(id=mch_id)
